package com.leave.interceptor;

import com.leave.mos.controller.LeaveDataController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserLoginInterceptor implements HandlerInterceptor {

    private final static Logger logger = LoggerFactory.getLogger(LeaveDataController.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(true);
        Object username = session.getAttribute("user");
        if (username != null) {
            //已登录
            return true;
        } else {
            //未登录
            //直接重定向到登录页面
            response.sendRedirect("/login");
            logger.info("未登录,重定向到首页。");
            return false;
        }
    }
}